package com.jushu.storbox.actions;

import android.content.Context;

import com.jushu.storbox.constant.Constant;
import com.jushu.storbox.database.DAOFactory;
import com.jushu.storbox.database.IFolderDao;
import com.jushu.storbox.entities.FileFolderInfo;

/**
 * Created by Tang on 2016/9/27.
 */
public class VirtureProgress implements Runnable {
    FileFolderInfo info;
    Context context;
    public VirtureProgress(Context context, FileFolderInfo info) {
        this.info = info;
        this.context = context;
    }

    public void run() {
        if(info != null) {
            for(int i=0;i<=100;i++) {
                info.setProgress(i);
                info.setTransStatus(Constant.DOWNLOAD_ISRUNNING_UPDATE_DB);
                info.notifyChange();
                if(!info.isSync()) {
                    break;
                }
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            IFolderDao folderDao = DAOFactory.instance(context).getFolderDao();
            if(info.isSync()) {
                folderDao.updateTransStatus(info.getOwnerId(), info.getId(), Constant.DOWNLOAD_SUCCESS_UPDATE_DB);
                info.setTransStatus(Constant.DOWNLOAD_SUCCESS_UPDATE_DB);
                info.notifyChange();
            } else {
                folderDao.updateTransStatus(info.getOwnerId(), info.getId(), Constant.DOWNLOAD_NON_UPDATE_DB);
                info.setTransStatus(Constant.DOWNLOAD_NON_UPDATE_DB);
                info.notifyChange();
            }
        }
    }
}
